#include "CGIAPI.hpp"

void CGIAPI::htmlHeadBegin(const char* title1,const char* charset1)
{
    printf("<html> \
            <head>  \
                <title>%s</title> \
                <meta charset=\"%s\"> \
            </head> \
            <body>",title1,charset1);
}

void CGIAPI::addStyle(int argv,...)
{
    string code;
    va_list args;
    va_start(args,argv);
    for (int i = 0; i < argv; i++)
    {
        code = code + R"(<link rel="stylesheet" type="text/css" href=")";
        code = code + va_arg(args,const char*) + "\">";
    }
    printf("%s",code.c_str());
}

void CGIAPI::addJavaScript(int argv,...)
{
    string code;
    va_list args;
    va_start(args,argv);
    for (int i = 0; i < argv; i++)
    {
        code = code + R"(<script type="text/javascript" src=")" + string(va_arg(args,const char*));
        code = code + "\"></script>";
    }
    va_end(args);
    printf("%s",code.c_str());
}

void CGIAPI::htmlHeadEnd()
{
    printf("</body></html>");
}

CGIAPI::CGIAPI()
{
    m_data.resize(3000);
    m_method = getenv("REQUEST_METHOD");
    if (m_method == "GET") m_data = getenv("QUERY_STRING");
    else if (m_method == "POST") fread(&m_data[0], sizeof(char), atoi(getenv("CONTENT_LENGTH")), stdin);
    else m_data = "";

    m_agent = getenv("HTTP_USER_AGENT");
    if (m_agent.find("Android") != string::npos) m_system = System::Android;
    else if (m_agent.find("Windows") != string::npos) m_system = System::Windows;
    else if (m_agent.find("Linux") != string::npos) m_system = System::Linux;
    else if (m_agent.find("Macintosh") != string::npos) m_system = System::Mac;
    else if (m_agent.find("iPhone") != string::npos) m_system = System::iPhone;
    else m_system = System::Other;
}

map<string,string> CGIAPI::getAllParam()
{
    map<string,string> m;
    vector<string> t1 = split(m_data.c_str(), "&");
    for (int i = 0;i < t1.size();i++)
    {
        vector<string> mq = split(t1[i].c_str(),"=");
        if (mq.size() == 1) m[mq[0]] = "";
        else m[mq[0]] = mq[1];
    }
    return m;
}

void CGIAPI::httpHeader(const char* value1)
{
    string value(value1);
    transform(value.begin(),value.end(),value.begin(),::tolower);
    if (value == "html")
        printf("Content-type:text/html\n\n");
    else if (value == "xml")
        printf("Content-type:text/xml\n\n");
}

vector<string> CGIAPI::split(const char *s1, const char *s2)
{
    string s = s1;
    string seperator = s2;

    vector<string> result;
    typedef string::size_type string_size;
    string_size i = 0;

    while(i != s.size())
    {
        int flag = 0;
        while(i != s.size() && flag == 0){
            flag = 1;
            for(string_size x = 0; x < seperator.size(); ++x)
                if(s[i] == seperator[x])
                {
                    ++i;
                    flag = 0;
                    break;
                }
        }

        flag = 0;
        string_size j = i;
        while(j != s.size() && flag == 0)
        {
            for(string_size x = 0; x < seperator.size(); ++x)
                if(s[j] == seperator[x]){
                    flag = 1;
                    break;
                }
                if(flag == 0) 
                    ++j;
        }
        if(i != j)
        {
            result.push_back(s.substr(i, j-i));
            i = j;
        }
    }
    return result;
}